package com.course.servlet;

import com.course.entity.Course;
import com.course.entity.Teacher;
import com.course.service.CourseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/laoshi_my_courses")
public class LaoshiMyCoursesServlet extends HttpServlet {
    private final CourseService courseService = new CourseService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        HttpSession session = request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if (teacher == null) {
            response.sendRedirect(request.getContextPath() + "/laoshi_login.jsp");
            return;
        }

        // 获取当前老师发布的所有课程
        List<Course> myCourses = courseService.getCoursesByTeacherId(teacher.getId());

        request.setAttribute("myCourses", myCourses);
        request.getRequestDispatcher("/laoshi_my_courses.jsp").forward(request, response);
    }
} 